<?php

namespace App;

use App\UserCoinLog;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'status',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getXYBAttribute()
    {
        return UserCoinLog::totalXyb( $this->id );
    }

    public function getJBAttribute()
    {
        return UserCoinLog::totalJb( $this->id );
    }

    public function game_zghpt()
    {
        if (!GameZghpt::where('user_id', $this->id)->exists()) {
            $gameZghpt = new GameZghpt;
            $gameZghpt->user_id = $this->id;
            $gameZghpt->quota = 0;
            $gameZghpt->level = 0;
            $gameZghpt->save();
        }
        return $this->hasOne('App\GameZghpt');
    }

    public function changes()
    {
        return $this->hasMany('App\UserChange');
    }

    public function orders()
    {
        return $this->hasMany('App\Order');
    }

    public function fans()
    {
        return $this->hasMany('App\User', 'finder_id');
    }

    public function game_ant_user()
    {
        return $this->hasOne('App\GameAntUser');
    }
}
